package com.avalon.shop.controller;

import com.avalon.core.model.RecordRow;
import com.avalon.shop.service.CommentService;
import com.avalon.shop.service.OrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;
    private final CommentService commentService;

    public OrderController(OrderService orderService, CommentService commentService) {
        this.orderService = orderService;
        this.commentService = commentService;
    }

    @PostMapping("/add")
    public void addOrder(@RequestBody Map<String, Object> value) {
        orderService.addOrder(value);
    }

    @PostMapping("/state/get")
    public List<Map> getOrderStateAll(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        String state = "";
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        if (param.containsKey("state")) {
            state = (String) param.get("state");
        }
        return orderService.getOrderStateAll(userId, state);
    }

    @PostMapping("/state/count")
    public Map getOrderStateCount(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }

        return orderService.getOrderStateCount(userId);
    }

    @PostMapping("/get/detail")
    public RecordRow getOrderDetail(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        return orderService.getOrderDetail(id);
    }

    @PostMapping("/receive/sure")
    public void orderSure(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        orderService.orderReceiveSure(id);
    }

    @PostMapping("/comment/complete")
    public void orderComment(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        orderService.orderComment(id);
    }

    @PostMapping("/comment/add")
    public void addOrderComment(@RequestBody List<Map<String, Object>> comments) {
        commentService.addOrderComment(comments);
    }

    @PostMapping("/custom/cancel")
    public void orderCancel(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        String backExpressNo = "";
        if (param.containsKey("backExpressNo")) {
            backExpressNo = param.get("backExpressNo").toString();
        }
        String backExpressCompany = "";
        if (param.containsKey("backExpressCompany")) {
            backExpressCompany = param.get("backExpressCompany").toString();
        }
        String backReason = "";
        if (param.containsKey("backReason")) {
            backReason = param.get("backReason").toString();
        }
        orderService.orderCancel(id, backExpressNo, backExpressCompany, backReason);
    }

    @PostMapping("/comment/get/last")
    public Map getLastProductComment(@RequestBody Map<String, Object> param) {
        Integer productId = 0;
        if (param.containsKey("productId")) {
            productId = Integer.parseInt(param.get("productId").toString());
        }
        return commentService.getLastProductComment(productId);
    }

    @PostMapping("/comment/get/count")
    public Map getProductCommentCount(@RequestBody Map<String, Object> param) {
        Integer productId = 0;
        if (param.containsKey("productId")) {
            productId = Integer.parseInt(param.get("productId").toString());
        }
        return commentService.getProductCommentCount(productId);
    }

    @PostMapping("/comment/get/page")
    public List<Map> getProductCommentPage(@RequestBody Map<String, Object> param) {
        Integer pageNum = 1;
        Integer pageSize = 10;
        Integer productId = 0;
        if (param.containsKey("pageNum")) {
            pageNum = Integer.parseInt(param.get("pageNum").toString());
        }
        if (param.containsKey("pageSize")) {
            pageSize = Integer.parseInt(param.get("pageSize").toString());
        }
        if (param.containsKey("productId")) {
            productId = Integer.parseInt(param.get("productId").toString());
        }
        return commentService.getProductCommentPage(productId, pageNum, pageSize);
    }
}
